const app = getApp();
const { AskPage } = app._require("common/index");
AskPage({
    data: {
        tableData: [],
        activeId: '',
        currentItem: null
    },
    onLoad: function (options) {
        if (options && options.id) {
            this.setData({
                activeId: options.id
            })
        }
    },
    onShow: function () {
        this.getData();
    },
    async getData() {
        const response = await this.$_server("searchData/getTownPeople", {
            data: {}
        });
        let { data } = response;
        let tableData = [];
        data.data.forEach(item => {
            let userList = []
            if (item.userList) {
                userList = item.userList
            }
            if (item.children) {
                item.children.forEach(it => {
                    if (it.userList) {
                        userList = userList.concat(it.userList)
                    }
                    if (it.children) {
                        it.children.forEach(i => {
                            if (i.userList) {
                                userList = userList.concat(i.userList)
                            }
                        })
                    }
                })
            }
            tableData.push({
                departmentName: item.name,
                children: userList
            })
        })
        tableData = tableData.filter(item => item.children.length)
        try {
            this.setData({
                tableData: tableData,
            });
        } catch (e) {}
    },
    selectItem(e) {
        const { item } = e.currentTarget.dataset
        this.setData({
            activeId: item.id,
            currentItem: item
        })
    },
    cancel() {
        this.setData({
            activeId: '',
            currentItem: null
        })
        wx.navigateBack({
            delta: 1
        });
    },
    sure() {
        const pages = getCurrentPages();
        const beforePage = pages[pages.length - 2];
        beforePage.setData({
            activeIdSelect: this.data.currentItem.id,
            currentItemSelect: this.data.currentItem
        });
        wx.navigateBack({
            delta: 1
        });
    }
});
